<html>
<script>

function doOperation(name='default', args=[]) {
    console.log("name: " + name + " args: " + args);
    if( typeof args == "object"){
    	for( i in args){
    		console.log("attribute: " + i + " value: " + args[i]);
    	}
    }
}

function findSome() {
    return Promise.resolve('Jerry');
}

function getCarList() {
    return Promise.resolve({size:4800, seats:5, output:6.0});
}
/*
findSome().then(args => {
    doOperation('searchCar', args);
});

getCarList().then(args => {
    doOperation('buyLexus', args);
});

//修改过程
//先通过bind函数，将操作方法从参数导入的方式分离成2个函数
var play = doOperation.bind(null, 'searchCar');
var buy = doOperation.bind(null, 'buyLexus');

findSome().then(play);
getCarList().then(buy);

*/

function runAsync(){
    var p = new Promise(function(resolve, reject){
        //做一些异步操作
        setTimeout(function(){
            console.log('执行完成');
            resolve('随便什么数据');
        }, 2000);
    });
    var a = "Jerry";
    return p;            
}
runAsync().then(function(data){
    console.log("Jerry in then callback: " + data);
    //后面可以用传过来的数据做些其他操作
    //......
});
console.log("after runAsync");

/*
after runAsync
执行完成
Jerry in then callback: 随便什么数据

*/

function printAfterTimeout(string, timeout){
  return new Promise((resolve, reject) => {
    setTimeout(function(){
      resolve(string);
    }, timeout);
  });
}

printAfterTimeout('<Hello> ', 2000).then((result) => {
  console.log(result);
  return printAfterTimeout(result + '<World>', 3000);
}).then((result) => {
  console.log("In then...");
  console.log(result);
});

</script>
</html>